package com.godpaper.as9.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.godpaper.as9.bussiness.delegates.UserDelegate;
	import com.godpaper.as9.constants.ServiceInfoList;
	import com.godpaper.as9.events.GetAlbumInfoEvent;
	import com.godpaper.as9.events.GetUserInfoEvent;
	import com.godpaper.as9.events.TryLoginEvent;
	import com.godpaper.as9.model.UserModelLocator;
	
	import mx.rpc.IResponder;

	public class TryLoginCommand extends CommandBase implements ICommand, IResponder
	{
		private var delegate:UserDelegate;
		private var userModel:UserModelLocator = UserModelLocator.getInstance();
		private var tryLoginEvent:TryLoginEvent;
		public function TryLoginCommand()
		{
			super();
			delegate = new UserDelegate(this);
		}

		override public function execute(event:CairngormEvent):void
		{
			tryLoginEvent = TryLoginEvent(event);
			
			delegate.tryLogin(ServiceInfoList.OPERATION_TRY_LOGIN,
								tryLoginEvent.simpleUser.email,
								tryLoginEvent.simpleUser.password);
		}
		
		override public function result(data:Object):void
		{
			if(data.hasOwnProperty("result"))
			{
				switch(data.result)
				{
					case "success":
						userModel.status = true;
						userModel.step = userModel.STEP_EXPLORER;
						userModel.simpleUser = tryLoginEvent.simpleUser;
						userModel.uploadURLVariables["email"] = tryLoginEvent.simpleUser.email;
						userModel.uploadURLVariables["ano"] = 5;
						CairngormEventDispatcher.getInstance().dispatchEvent(new GetUserInfoEvent());
						CairngormEventDispatcher.getInstance().dispatchEvent(new GetAlbumInfoEvent());
						break;
					case "failure":
						userModel.status = false;
						break;
					default:
						break;		
				}
			}
			
		}
		
	}
}